<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use DB;
use App\Models\Art;
use Auth;
use Captcha;
// use Markdown;
use Validator;
class ArticleController extends Controller
{   
    public function display(){
        $art = Art::orderBy('id','desc')->paginate(2);
        return view('art.display',['art'=>$art]);
    }

    public function getData(){
        $id=$_GET;
        // $art = Art::count();
        // if($_GET<$art){
        //     // $data=Art::limit(1);
            //$data = Art::select("select * from art limit {$page},1");
        //     // var_dump($data);
        //     // $data=$_GET;
        //     $data=$art;
        //     // $data=Art::where("id",1)->get();
        $art = Art::find($id);
        // }else{
        //      $art=false;
        // }
        // $data=$_GET;
         
        return $art;
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        // $art = DB::table('art')->orderBy('id','desc')->paginate(2);
        $art = Art::orderBy('id','desc')->paginate(2);
        return view('art.index',['art'=>$art]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('art/add');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $data = $request->except('_token');
        // dd($data);
        // $res = DB::table('art')->insert($data);

        //验证方式
        //1.门面validate验证
/*        $val = Validator::make($request->all(),[
                'title'=>'required|max:30|min:8',
                'content'=>'required|max:1000|min:50',
            ]);
        
        if($val->fails()){
            return back()->withErrors($val)->withInput();
        }*/

        //验证方式2 trait 自带的验证

        $this->validate($request,[
                'title' => 'required|max:30|min:8',
                'content' => 'required|max:1000|min:50',
            ],[
                'required'=>':attribute 是必填的',
            ]);

        $res = Art::create(['title'=>$request->title,'content'=>$request->content]);
        if($res){
            return redirect('/art');
        }else{
            return back();
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        // $art = DB::table('art')->where('id',$id)->first();
        $art = Art::find($id);
        // $art ->content = Markdown::convertToHtml($art->content);
        return view('art.show',['art'=>$art]);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        // $art = DB::table('art')->where('id',$id)->first();
        $art = Art::find($id);
        // dd($art);
        // $art['fillable'][1]  = htmlspecialchars($art['fillable'][1]);
        // dd($art['fillable'][1]);
        return view('art/edit',['art'=>$art]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        // $data = $request->title;
        // $data = $request->content;
        // var_dump($data);
        // $res = DB::table('art')->where('id',$id)->update(['title'=>$request->title,'content'=>$request->content]);
        $art = Art::where('id',$id)->update($request->except('_token','_method'));
        return redirect('/art');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        // $res = DB::table('art')->where('id',$id)->delete();
        //方一
        // $art = Art::find($id)->delete();

        //方二
        $art = Art::destroy($id);
        return redirect('/art');
    }



    public function postLogin(Request $request)
    {
        $res = Auth::attempt(['email'=>$request->email,'password'=>$request->password]);
        if($res){
            return redirect('/');
        }else{
            return back();
        }

    }

    public function getUser()
    {
        // dd(Auth::user());
        dd(Auth::check());
    }

    public function getLogout()
    {
        Auth::logout();
    }


    //验证码方法
    public function getCode()
    {
        // echo Captcha::img();
        return Captcha::src();
    }

    public function getVC()
    {
        echo Captcha::generate();
    }

}
